<?php
// TODO: dodawanie, edytowanie, usuwanie vortal
session_start();
class Controller_Admin_Vortal extends Controller_Admin_Abstract
{
	public function __construct(Controller_Front $frontController)
	{
		parent::__construct('admin_vortal', $frontController);
	}	
	
	public function indexAction()
	{
		$this->view()->display('admin_vortals.tpl');
	}
	
	public function newAction()
	{
		$vortals;
		$buttonName;
		
		if (isset($_GET['id_vortal'])) {
			$id = $_GET['id_vortal'];
			$buttonName = 'Edytuj';
			
			$vortals = VortalsPeer::retrieveByPK($id);
		} else {
			$buttonName = 'Dodaj';
			$vortals = new Vortals();
		}
		$this->view()->assign('vortals', $vortals);
		$this->view()->assign('editButton', $buttonName);
		$_SESSION['enable']=true;
		$this->view()->display('admin_vortals_edit.tpl');
	}
	
	public function listAction()
	{
		$vortals = VortalsPeer::doSelect(new Criteria());
		//var_dump($categories);
		$this->view()->assign('vortals', $vortals);
		
		$this->view()->display('admin_vortals_list.tpl');
	}
	
	public function checkAction()
	{		
		$vortals;
		
		if (!empty($_POST['id_vortal'])) {
			$id = $_POST['id_vortal'];
			
			$vortals = VortalsPeer::retrieveByPK($id);
		} else {
			$vortals = new Vortals();
		}
		
		if($_SESSION["enable"]==true) {
			$vortals->setName($_POST['name']);
			$vortals->setUrl($_POST['url']);
			$vortals->setLogo($_POST['logo']);
			//var_dump($category->validate('name'));
			$vortals->setIdRequestStatus(1);
			$vortals->save();
			$this->ok('Vortal saved.');
			$_SESSION['enable']=false;
		} else {
			$this->fail('Re-saving it is not possible.');
		}
	}	
	
	public function deleteAction()
	{
		
		if (!isset($_GET['id_vortal'])) {
			throw new Exception('id not spec');
		}	
		
		$id = $_GET['id_vortal'];
		try {
			VortalPeer::doDelete($id);
		} catch (Exception $e) {
			$this->view()->assign('fail_info', 'Nie uda�o si� usun�� ' 
				. $e->getMessage());  	
			return $this->view()->display('fail.tpl');		
		}
				
			
	}
	
}